	var arrayBool = [];

	function validateUsername(){

		var username=document.getElementById("username");
	
		if(username.value == "" || username.value == null || username.value.length<5) {
		  username.style.background = '#f27b75'; 
		  showHide("usernameerror");
		  username.focus();
		  arrayBool[0] = false;
		  validateAll();
		  return false;
		}
		
		re = /^\w+$/;
		if(!re.test(username.value)) {
		  username.style.background = '#f27b75';
		  showHide("usernameerror");
		  username.focus();
		  arrayBool[0] = false;
		  validateAll();
		  return false;
		}	
		username.style.background = 'White';
		hide("usernameerror");
		arrayBool[0] = true;
		validateAll();
		return true;
	}
	
	function validateEmail(){
		var x=document.getElementById("email");
		
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		if (emailPattern.test(x.value) == false){
			x.style.background = '#f27b75';
			showHide("emailerror");
			arrayBool[1] = false;
			validateAll();
			return false;
		} else {
			x.style.background = 'White';
			hide("emailerror");
			arrayBool[1] = true;
			validateAll();
			return true;
		}
	}
	
	function validateName(){
		var x=document.getElementById("fullname");
		var patt=/\w+\s+\w/g;
		//alert(x);
		if (x.value==null || x.value==" "){
			x.style.background = '#f27b75';
			showHide("fullnameerror");
			arrayBool[2] = false;
			validateAll();
			return false;
		} else if (patt.test(x.value) == false){
			x.style.background = '#f27b75';
			showHide("fullnameerror");
			arrayBool[2] = false;
			validateAll();
			return false;
		} else {
			x.style.background = 'White';
			hide("fullnameerror");
			arrayBool[2] = true;
			validateAll();
			return true;
		}
	}

	function validateDate(){
		var x=document.getElementById("date");
		var patt=/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;
		//alert(x);
		if (x.value==null || x.value==" "){
			x.style.background = '#f27b75';
			showHide("dateeerror");
			arrayBool[3] = false;
			validateAll();
			return false;
		} else if (patt.test(x.value) == false){
			x.style.background = '#f27b75';
			showHide("dateerror");
			arrayBool[3] = false;
			validateAll();
			return false;
		} else {
			x.style.background = 'White';
			hide("dateerror");
			arrayBool[3] = true;
			validateAll();
			return true;
		}
	}
	
	function checkPwd(){
		var pwd1=document.getElementById("pwd1");
		var pwd2=document.getElementById("pwd2");
		var username=document.getElementById("username");
		var email=document.getElementById("email").value;
		var mail=email.substr(0,email.indexOf("@"));
		
		if(pwd1.value != "" && pwd1.value == pwd2.value) {
			if(pwd1.value.length < 8) {
				pwd1.style.background = '#f27b75';
				pwd2.style.background = '#f27b75';
				showHide("pwderror");
				//alert("Error: Password must contain at least eight characters!");
				pwd1.focus();
				arrayBool[4] = false;
				validateAll();
				return false;
			}
			
			if(pwd1.value == username.value) {
				pwd1.style.background = '#f27b75';
				pwd2.style.background = '#f27b75';
				showHide("pwderror");
				//alert("Error: Password must be different from Username!");
				pwd1.focus();
				arrayBool[4] = false;
				validateAll();
				return false;
			}
			
			if(pwd1.value == mail){
				pwd1.style.background = '#f27b75';
				pwd2.style.background = '#f27b75';
				showHide("pwderror");
				//alert("Error: Password must be different from Email!");
				pwd1.focus();
				arrayBool[4] = false;
				validateAll();
				return false;
			}
		} else {
			pwd1.style.background = '#f27b75';
			pwd2.style.background = '#f27b75';
			showHide("pwderror");
			//alert("Error: Please check that you've entered and confirmed your password!");
			pwd1.focus();
			arrayBool[4] = false;
			validateAll();
			return false;
		}
        pwd1.style.background = 'White';
		pwd2.style.background = 'White';
		hide("pwderror");
		arrayBool[4] = true;
		validateAll();
		//alert("You entered a valid password: " + pwd1.value);
		return true;	
	}
  
	function validateAvatar(){
		var avatar=document.getElementById("avatar");
		var titik=avatar.value.indexOf(".");
		var last=avatar.value.substr(titik+1);
		
		if (last=="jpg" || last=="jpeg"){
			hide("avatarerror");
			arrayBool[5] = true;
			validateAll();
			return true;
		} else {
			showHide("avatarerror");
			arrayBool[5] = false;
			validateAll();
			return false;
		}
	}
	
	function validateGender(){
		var gender=document.getElementById("sex");
		
		if (gender.value=="0"){
			showHide("gendererror");
			arrayBool[6] = false;
			validateAll();
			return false;
		} else {
			hide("gendererror");
			arrayBool[6] = true;
			validateAll();
			return true;
		}
	}

	function validateAll(){
		var bool = true;
		var i=0;
		for(i=0;i<7;i++){
			if (arrayBool[i]==false || arrayBool[i]==null){
				bool = false;
			}
		}
		if(bool==true){
			enReg();
		} else { //bool=false
			disReg();
		}
	}
	
	function disReg(){
		document.getElementById("submitForm").disabled = true;
	}
	
	function enReg(){
		document.getElementById("submitForm").disabled = false;
	}
	
	function showHide(obj){
		var div = document.getElementById(obj);
	
		if (div.style.display == 'none') {
			div.style.display = '';
		} else {
			div.style.display = 'none';
		}
	}
	
	function hide(obj){
		var div = document.getElementById(obj);
		
		if (div.style.display == '') {
			div.style.display = 'none';
		} else {
			div.style.display = 'none';
		}
	}
	
	function succeed(){
		alert("Welcome, "+document.getElementById("username").value);
	}